const app = getApp()
const api = require('../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},
    data: {
      imagesUrl: app.globalData.imagesUrl,
      siteUrl: app.globalData.siteUrl,
      showView: true,
      // 全选
      result: [],
      checked: false,
      productList:''
    },
    // 全选
    checkboxChange(e) {
      this.setData({
        result: e.detail,
      });
      let b = [];
      for(let i = 0; i<this.data.productList.length; i++){
        b.push(this.data.productList[i].id)
      }
      let c = e.detail.length === b.length && e.detail.filter(t => !b.includes(t));
      if (c) {
        this.setData({
          checked: true,
        });
      } else {
        this.setData({
          checked: false,
        });
      }
    },
    checkAll(e) {
      this.setData({
        checked: e.detail,
      });
      if (e.detail) {
        let b = [];
        for(let i = 0; i<this.data.productList.length; i++){
          b.push(this.data.productList[i].id)
        }
        this.setData({
          result: b,
        });
      } else {
        this.setData({
          result: [],
        });
      }
    },
    onShow: function () {
      // 生命周期函数--监听页面加载
      showView: true
      this.setData({
        page: 1
      })
      let that = this
      this.get_prodcut(that, 1)
    }, 

    get_prodcut(that, page){
      let param = {
        ucode : app.globalData.userinfo.ucode
      }
      api.product_index(param, page).then(function(res){
        if (res.data.length>0){
          that.setData({
            productList: page == 1 ? res.data : that.data.productList.concat(res.data),//合并数据
            page: page + 1
         },()=>{
           
         })
        }else{
            wx.showToast({
              title: '没有更多数据了',
              ico: 'error'
            })
        }
        
    })
    }, 
     

    onChangeShowState: function () {
      var that = this;
      that.setData({
        showView: (!that.data.showView)
      })
    },
    submitHandle: function(){
       wx.redirectTo({
         url: '/pages/serviced/edit/edit',
       })
    },
    del: function(){
      let that = this
      let param = {
        ids:this.data.result
      }
      api.del_product(param).then(function(res){
          wx.redirectTo({
            url: '/pages/serviced/serviced',
          })
      })
    },

    onPullDownRefresh:function(){
      this.onRefresh();
    },
    onRefresh:function(){
        let that = this
        //导航条加载动画
        wx.showNavigationBarLoading();
        setTimeout(function () {
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          that.setData({
            page: 1
          })
          that.get_prodcut(that, 1)
          wx.stopPullDownRefresh();
        }, 2000);
      },

      onReachBottom:function(){
       wx.hideLoading()
        // 加载更多，滑动到底部执行此方法
        let page = this.data.page ;
        console.log(page)
        let that = this
        this.get_prodcut(that, page)
    }

  })